﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using YuShiManageSystem.DATA;
using YuShiManageSystem.COM;


namespace YuShiManageSystem.UI.Card
{
    /// <summary>
    /// 开卡窗体
    /// </summary>
    //当点击保存时，对充值金额进行判断，若充值金额小于面额时，给出是经理签单还是欠款提示，超出面额，报错
    public partial class MakeCard : YuShiManageSystem.UI.Base
    {
        #region 构造方法
        public MakeCard()
        {
            InitializeComponent();
        }
        public MakeCard(YuShiManageSystem.DATA.Session se)
            : base(se)
        {
            InitializeComponent();
            InitializeComboBox();
            this.code.Focus();
        } 
        #endregion
        #region 初始化ComboBox控件
        private void InitializeComboBox()
        {
            
            this.cmbCardType.DataSource = this.Session.DataModel.tb_cardType;            
            this.cmbCardType.DisplayMember = "typeName";
            this.cmbCardType.ValueMember = "typeId";
            this.cmbCardType.SelectedIndex = -1;
        } 
        #endregion
        #region ComboBox事件
        private void cmbCardType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cmbCardType.SelectedIndex == -1 || ((ComboBox)sender).SelectedValue.GetType() == typeof(tb_cardType)) return;
            int index = Convert.ToInt32(((ComboBox)sender).SelectedValue);
            tb_cardType type = this.Session.DataModel.tb_cardType.Where(t => t.typeId == index).FirstOrDefault();
            this.vipAmount.Text = type.amount.ToString();

        } 
        #endregion

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
            
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //对输入进行验证：小单号、卡类型选择、支付明细、员工提成
            
            //添加至消费单表
            try
            {
                List<System.Data.Objects.DataClasses.EntityObject> entities = new List<System.Data.Objects.DataClasses.EntityObject>();
                entities.Add(GetOrderByUI());//消费单表
                entities.Add(GetMaintenanceByUI());//卡异动表
                entities.Add(GetCardByUI());//会员卡表
                entities.AddRange(GetBusinessByUI());//业绩表
                int i = this.Session.DataModel.Save(entities);
                ////保存成功后，在另一窗体中显示新卡信息，  
                if (i > 0)
                    new Form();
                else
                    throw new Exception("数据保存失败！");
            }
            catch (Exception ex)
            {
                YSMessage.Error(ex);
            }                  
        }

        #region 获取界面输入的数据
        private List<tb_business> GetBusinessByUI()
        {
            List<tb_business> business = new List<tb_business>();
            foreach (DataGridViewRow r in this.staffInfo.Rows)
            {
                //if (r.IsNewRow) continue;
                //tb_business b = new tb_business();
                //b.personNo=r.Cells[0].Value.ToString();
                //b.createDate = DateTime.Now.Date;
                //b.category = Session.RESULT_CASH;
                //b.complete = Convert.ToDecimal(r.Cells[1].Value);
                //b.amount = Convert.ToDecimal(r.Cells[2].Value);
                //business.Add(b);
            }
            tb_business b = new tb_business();
            b.personNo = "6660001";
            b.createDate = DateTime.Now.Date;
            b.category = Session.RESULT_CASH;
            b.complete = 3000;//Convert.ToDecimal(r.Cells[1].Value);
            b.amount = 30; //Convert.ToDecimal(r.Cells[2].Value);
            business.Add(b);
            return business;

        }
        private tb_card GetCardByUI()
        {
            tb_card card = null;
            //if (PayInfo.Rows.Count == 1) return card;
            card = GetCardByType(this.Session.DataModel.tb_cardType.Where(t => t.typeId == this.cmbCardType.SelectedIndex).FirstOrDefault());
            card.code = this.code.Text.Trim();
            card.storeId = this.Session.Store.storeId;
            card.useAmount = 5000; //Convert.ToDecimal(this.PayInfo.Rows[0].Cells[0].Value);
            card.debt = card.vipAmount - card.useAmount;
            card.useTimes = card.times;
            card.delFlg = Session.DELFLAG_NO;
            card.status = Session.CARD_STATE_YES;
            card.openDate = DateTime.Now.Date;
            card.customerNo = 100003;//(this.Session.DataModel.tb_customer.OrderBy(d => d.customerNo).Select(d => d.customerNo));
            return card;
        }
        /// <summary>
        /// 根据选择会员卡的类型返回一个会员卡
        ///</summary>
        ///  <param name="type"></param>
        ///  <returns></returns>
        private tb_card GetCardByType(tb_cardType type)
        {
            tb_card card = null;
            if (type == null) return card;
            card = new tb_card();
            card.amount = type.amount;
            card.times = type.times;
            card.typeId = type.typeId;
            return card;
        }
        private tb_maintenance GetMaintenanceByUI()
        {
            tb_maintenance miantenance = null;
            miantenance = new tb_maintenance();

            using (DataModel model = new DataModel())
            {
                miantenance.cardId = model.tb_card.Any() ? model.tb_card.Max(c => c.cardId) + 1 : 0;
            }

            miantenance.changeDate = DateTime.Now.Date;
            miantenance.changeType = Session.CARD_OPEN;
            miantenance.amount = 5000;//Convert.ToDecimal(this.PayInfo.Rows[0].Cells[0].Value);
            using (DataModel model = new DataModel())
            {
                miantenance.orderNo = model.tb_order.Any() ? model.tb_order.Max(o => o.orderNo) + 1 : 0;
            }
            miantenance.person = "";
            return miantenance;
        }
        private tb_order GetOrderByUI()
        {
            tb_order order = null;
            order = new tb_order();
            order.storeId = this.Session.Store.storeId;
            using (DataModel model = new DataModel())
            {
                order.cardId = model.tb_card.Any() ? model.tb_card.Max(c => c.cardId) + 1 : 0;
            }

            //order.orderNo=this.Session.DataModel.tb_order.Any()?:
            order.customerType = Session.CUSTOMER_TYPE_VIP;
            order.subCode = this.subCode.Text.Trim();
            order.createDate = DateTime.Now.Date;
            order.remark = this.remark.Text.Trim();
            order.person = "";

            return order;
        } 
        #endregion

        
        
    
    }

   
}
